package com.hrs.core.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hrs.common.result.Result;
import com.hrs.core.mapper.AchieveMapper;
import com.hrs.enity.db.Position;
import com.hrs.core.service.PositionService;
import com.hrs.core.mapper.PositionMapper;
import com.hrs.enity.ro.PositionAddRo;
import com.hrs.enity.ro.PositionUpdateRo;
import com.hrs.enity.vo.PositionVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
* @author 纱雾休比酱
* @description 针对表【t_position(岗位表)】的数据库操作Service实现
* @createDate 2023-01-12 22:17:10
*/
@Service
public class PositionServiceImpl extends ServiceImpl<PositionMapper, Position>
    implements PositionService{

    @Autowired
    private PositionTreeService positionTreeService;

    @Autowired
    private PositionMapper positionMapper;

    @Autowired
    private AchieveMapper achieveMapper;

    @Override
    public Result<List<PositionVo>> listPosition() {
        List<PositionVo> positionVos = positionMapper.listAll();
        List<PositionVo> result = positionTreeService.buildPositionTree(positionVos);
        return Result.success(result);
    }

    @Override
    public Result<List<PositionVo>> listAll() {
        List<PositionVo> positionVos = positionMapper.listAll();
        return Result.success(positionVos);
    }

    @Override
    public Result<PositionVo> getPositionById(Integer pid) {
        PositionVo positionVo = positionMapper.selectByPid(pid);
        return Result.success(positionVo);
    }

    @Override
    public Result<String> addPosition(PositionAddRo positionAddRo) {
        Position position = new Position();
        BeanUtils.copyProperties(positionAddRo,position);
        this.save(position);
        position.setSort(position.getId());
        this.updateById(position);
        return Result.success();
    }

    @Override
    public Result<String> updatePosition(PositionUpdateRo positionUpdateRo) {
        if(positionUpdateRo.getParentId() == null) {
            return Result.error("上级岗位id不能等于当前岗位id");
        }
        Position position = new Position();
        BeanUtils.copyProperties(positionUpdateRo,position);
        this.updateById(position);
        return Result.success();
    }

    @Override
    public Result<String> delPosition(Integer pId) {
        Position position = this.getById(pId);
        if(Objects.isNull(position)) {
            return Result.error("目标不存在");
        }

        Integer countSubPosition = positionMapper.countSubPosition(pId);
        if(countSubPosition > 0) {
            return Result.error("存在子岗位，不可删除");
        }

        Integer countByPositionId = achieveMapper.countByPositionId(pId);
        if(countByPositionId > 0) {
            return Result.error("与档案有关联，不可删除");
        }

        this.updateById(position);
        return Result.success();
    }
}




